﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Org.BouncyCastle.Utilities.IO.Pem;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;

namespace CommModule.Security
{
    public class CertificateUtils
    {
        public static RSACryptoServiceProvider GetCertcopPublicKey()
        {
            X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadOnly);

            foreach (X509Certificate2 mCert in store.Certificates)
            {
                if (mCert.SubjectName.Name.Equals("CN=CERTCOP Application Server, OU=CERTCOP Security Services, O=CERTCOP Client, L=Oeiras, S=Lisbon, C=PT"))
                    return (RSACryptoServiceProvider)mCert.PublicKey.Key;
            }
            return null;
        }

        public static RSACryptoServiceProvider GetServerPrivateKey()
        {
            X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadOnly);

            foreach (X509Certificate2 mCert in store.Certificates)
            {
                if (mCert.SubjectName.Name.Equals("CN=CERTCOP Web Server, OU=CERTCOP Security Services, O=CERTCOP Client, L=Oeiras, S=Lisbon, C=PT"))
                    return (RSACryptoServiceProvider)mCert.PrivateKey;
            }
            return null;
        }

        public static RSACryptoServiceProvider GetServerPublicKey()
        {
            X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadOnly);

            foreach (X509Certificate2 mCert in store.Certificates)
            {
                if (mCert.SubjectName.Name.Equals("CN=CERTCOP Web Server, OU=CERTCOP Security Services, O=CERTCOP Client, L=Oeiras, S=Lisbon, C=PT"))
                    return (RSACryptoServiceProvider)mCert.PublicKey.Key;
            }
            return null;
        }
    }
}
